#pragma once
#include<iostream>
#include<string>
#include<pthread.h>
namespace LockModule
{
    class Mutex
    {
        public:
        Mutex()
        {
            pthread_mutex_init(&_mutex,nullptr);
        }
        Mutex(const Mutex& mutex)=delete;//锁不允许拷贝
        Mutex operator=(const Mutex& mutex)=delete;
        void Lock()
        {
            pthread_mutex_lock(&_mutex);
        }
        void Unlock()
        {
            pthread_mutex_unlock(&_mutex);
        }
        pthread_mutex_t* GetMutexOriginal()
        {
            return &_mutex;
        }
        ~Mutex()
        {
            pthread_mutex_destroy(&_mutex);
        }
        private:
        pthread_mutex_t _mutex;
    };

    //RAII风格（资源获取即初始化）
    //将锁的初始化与对象的初始化进行绑定，锁的销毁与对象的析构进行绑定 
    class LockGuard
    {
        public:
        LockGuard(Mutex& mutex)
        :_mutex(mutex)
        {
            _mutex.Lock();
        }
        ~LockGuard()
        {
            _mutex.Unlock();
        }
        private:
        Mutex& _mutex;
    };
}